<template>
  <div class="create-interview-content">
    <el-form ref="interviewForm" :model="form" :rules="rules" label-position="left" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="面试职位" prop="position">
            <el-select size="small" v-loading="positionLoading" v-model="form.position" placeholder="请选择面试职位"
                       :filterable="true">
              <el-option
                  v-for="item in position"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="面试方式">
            <el-input size="small" value="视频面试" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="联系人">
            <el-input size="small" :value="phone" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="面试日期" prop="date">
            <el-date-picker v-model="form.date" type="date" placement="bottom-start"
                            :picker-options="{disabledDate:timeStart}" size="small" placeholder="请选择面试日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="面试时间" prop="time">
            <el-time-select size="small" placement="bottom-start" :disabled="!this.form.date" v-model="form.time"
                            :picker-options="{start: dateStart,step: '00:10',end: '20:00'}"
                            placeholder="请选择面试时间">
            </el-time-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注事项">
            <el-input
                maxlength="140"
                type="textarea"
                :rows="2"
                placeholder="请输入备注事项"
                v-model="form.remarks">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {getPositionList, submitInterview} from "@/api/interview";
import Vue from "vue";
import {getPhoneByImUserID, getToAccount} from "@/utils/im";

export default {
  name: "CreateInterview",
  props: ['phone', 'conversation'],
  data() {
    return {
      form: {
        position: null,
        date: '',
        time: '',
        remarks: '',
      },
      rules: {
        position: [{required: true, trigger: 'blur', message: '请选择面试岗位'}],
        date: [{required: true, trigger: 'blur', message: '请选择面试日期'}],
        time: [{required: true, message: '请选择面试时间'}],
      },
      position: [],
      positionLoading: true,
    }
  },
  computed: {
    dateStart() {
      let time = '08:00';
      if (this.form.date) {
        const utc = new Date(this.form.date);
        let date = this.utils.utcToTime(utc, 'd');
        let current = this.utils.getCurrentTime('d');
        if (date === current) {
          time = this.utils.getCurrentTime('i').substring(11, 16);
          this.form.time = time;
        }
      }
      return time;
    }
  },
  methods: {
    getPosition() {
      getPositionList().then((res) => {
        if (res.length === 0) {
          this.$message.warning('无可选择的岗位，请在职位管理发布')
          return;
        }
        let position = [];
        for (let i = 0; i < res.length; i++) {
          const current = res[i];
          position.push({
            id: current.id,
            name: current.jobTitle + ' ' + current.minSalary + ' - ' + current.maxSalary + '元'
          })
        }
        this.position = position;
      }).finally(() => {
        this.positionLoading = false;
      })
    },
    submit(dialogVisible) {
      this.$refs.interviewForm.validate(valid => {
        if (valid) {
          this.$confirm('面试前3小时，双方可申请取消\n如不取消，请按时出席\n爽约乙方，平台会打【不良记录】', '面试诚信原则', {
            cancelButtonText: '我在想想',
            confirmButtonText: '不爽约'
          }).then(() => {
            const loading = Vue.prototype.$loading({
              fullscreen: true,
              text: '加载中',
            })
            const form = {
              remarks: this.form.remarks,
              interviewTime: this.utils.utcToTime(this.form.date, 'd') + ' ' + this.form.time,
              pid: this.form.position,
              mobile: getPhoneByImUserID(getToAccount(this.conversation.conversationID, this.conversation.type)),
              plan: 1,
              id: 0
            }
            submitInterview(form).then((res) => {
              const id = res.id;
              const message = this.tim.createCustomMessage({
                to: getToAccount(this.conversation.conversationID, this.conversation.type),
                conversationType: this.TIM.TYPES.CONV_C2C,
                payload: {
                  data: 'invited',
                  description: '发送面试邀请',
                  extension: JSON.stringify({id, agree: null})
                }
              })
              this.tim.sendMessage(message).then(() => {
                loading.close();
                this.$message({
                  message: '操作成功',
                  type: "success",
                  duration: 2000,
                  onClose: () => {
                    this.$bus.$emit('sendMessage', {
                      detail: {
                        message
                      }
                    });
                    dialogVisible[0] = false;
                  }
                })
              }).catch(() => {
                loading.close();
                this.$message.error('操作失败')
              })
            }).catch(() => {
              loading.close();
              this.$message.error('操作失败')
            })
          })
        } else {
          return false
        }
      })
    },
    timeStart(e) {
      let date = new Date(e).getTime() / 1000;
      let current = new Date(new Date().toLocaleDateString()).getTime() / 1000
      return date < current;
    }
  },
  mounted() {
    this.getPosition();
  }
}
</script>

<style scoped>

</style>
